<template>
  <div class="service-cards">
    <div class="cards-container">
      <div class="service-card"
           v-for="(card, index) in serviceCards"
           :key="index"
           :class="classData[index]"
           @click="handleCardClick(card)">
        <div class="card-icon">
          <img :src="card.icon" alt="service icon">
        </div>
        <div class="card-content">
          <div class="card-title">
            <span>{{ card.name }}</span>
          </div>
          <div class="card-desc"
               ref="welcomeDesc"
               @mouseenter="handleMouseEnter($event)"
               @mouseleave="handleMouseLeave">
            {{ card.remark }}
          </div>
        </div>
      </div>
    </div>

    <!-- 全局tooltip -->
    <div class="global-tooltip"
         ref="tooltip"
         v-show="showTooltip"
         :style="tooltipStyle">
      {{ tooltipText }}
    </div>
  </div>
</template>

<script>
export default {
  name: 'ServiceCards',
  data() {
    return {
      tooltipText: '',
      showTooltip: false,
      tooltipStyle: {
        left: '0px',
        top: '0px'
      },
      classData: ['welcome', 'service', 'community', 'contact'],
      serviceCards: this.$cache.getItem("middleCategory"),

    }
  },
  methods: {
    handleMouseEnter(event) {
      const el = event.target
      const text = el.textContent.trim()

      // 检查内容是否超出
      const isOverflowing = el.offsetWidth < el.scrollWidth

      if (isOverflowing) {
        this.tooltipText = text
        // 计算位置
        const rect = el.getBoundingClientRect()
        this.tooltipStyle = {
          left: `${rect.left + rect.width / 2}px`,
          top: `${rect.top - 8}px`
        }
        this.showTooltip = true
      }
    },
    handleMouseLeave() {
      this.showTooltip = false
      this.tooltipText = ''
    },
    handleCardClick(item) {
      if (item.path) {
        if (item.event == 2) {
          this.$router.push({path: item.path});
        } else if (item.event == 1) {
          window.open(item.path, '_blank');
        }
      } else {
        console.log('跳转参数为空')
      }
    }
  }
}
</script>

<style scoped>
.service-cards {
  width: 100%;
}

.cards-container {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 15px;
  padding: 0 15px;
}

.service-card {
  position: relative;
  padding: 15px;
  border-radius: 8px;
  transition: all 0.3s;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 12px;
  overflow: hidden;
  animation: cardFadeIn 0.3s ease-out;
}

/* 卡片主题样式 */
.welcome {
  background: linear-gradient(135deg, #ff758c 0%, #ff7eb3 100%);
}

.service {
  background: #2c3e50;
}

.community {
  background: #4a90e2;
}

.contact {
  background: #ff9f43;
}

.service-card:hover {
  transform: translateY(-4px);
  box-shadow: 0 8px 16px rgba(0, 0, 0, 0.15);
}

.card-icon {
  width: 40px;
  height: 40px;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  background: rgba(255, 255, 255, 0.2);
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

.card-icon img {
  width: 24px;
  height: 24px;
  filter: drop-shadow(0 1px 2px rgba(0, 0, 0, 0.1));
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

.card-content {
  flex: 1;
  min-width: 0;
  color: white;
  transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

.card-title {
  font-size: 15px;
  font-weight: 500;
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 4px;
}

.card-desc {
  font-size: 13px;
  color: rgba(255, 255, 255, 0.9);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  cursor: default;
  max-width: 100%;
}

.global-tooltip {
  position: fixed;
  background: rgba(0, 0, 0, 0.8);
  color: white;
  padding: 8px 12px;
  border-radius: 4px;
  font-size: 13px;
  white-space: normal;
  max-width: 300px;
  width: max-content;
  z-index: 1000;
  transform: translate(-50%, calc(-100% - 10px));
  pointer-events: none;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
  opacity: 1;
  transition: opacity 0.2s ease;
}

.global-tooltip.hidden {
  opacity: 0;
}

.global-tooltip::after {
  content: '';
  position: absolute;
  left: 50%;
  bottom: -6px;
  transform: translateX(-50%);
  border-left: 6px solid transparent;
  border-right: 6px solid transparent;
  border-top: 6px solid rgba(0, 0, 0, 0.8);
}

@media (max-width: 1200px) {
  .cards-container {
    grid-template-columns: repeat(2, 1fr);
    gap: 12px;
    padding: 0 10px;
  }
}

@media (max-width: 768px) {
  .cards-container {
    grid-template-columns: repeat(2, 1fr);
    gap: 10px;
    padding: 0 15px;
  }

  .service-card {
    padding: 15px;
    gap: 10px;
    min-height: 100px;
    flex-direction: column;
    text-align: center;
    justify-content: center;
  }

  .card-desc {
    max-width: none;
    width: 100%;
    font-size: 12px;
    white-space: normal;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
    text-overflow: ellipsis;
    line-height: 1.4;
    margin-top: 5px;
  }

  .card-title {
    font-size: 14px;
    width: 100%;
    justify-content: center;
  }

  .card-icon {
    width: 40px;
    height: 40px;
    margin-bottom: 8px;
  }

  .card-icon img {
    width: 24px;
    height: 24px;
  }

  .card-content {
    width: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;
  }

  .global-tooltip {
    display: none !important;
  }
}

/* 小屏幕适配 */
@media (max-width: 375px) {
  .cards-container {
    gap: 8px;
    padding: 0 10px;
  }

  .service-card {
    padding: 12px;
    min-height: 90px;
  }

  .card-icon {
    width: 36px;
    height: 36px;
    margin-bottom: 6px;
  }

  .card-icon img {
    width: 24px;
    height: 24px;
  }

  .card-title {
    font-size: 13px;
  }

  .card-desc {
    font-size: 11px;
    -webkit-line-clamp: 2;
  }
}

/* 自定义标题提示样式 */
.service-card[title] {
  position: relative;
}

.service-card[title]:hover::before {
  content: attr(title);
  position: fixed;
  padding: 8px 12px;
  background: rgba(0, 0, 0, 0.8);
  color: white;
  font-size: 13px;
  border-radius: 4px;
  max-width: 300px;
  word-wrap: break-word;
  z-index: 1000;
  pointer-events: none;
  left: 50%;
  transform: translateX(-50%);
}

/* 添加卡片动画效果 */
@keyframes cardFadeIn {
  from {
    opacity: 0;
    transform: translateY(10px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.service-card {
  animation: cardFadeIn 0.3s ease-out;
}

/* 修改图标旋转动画 */
.service-card:hover .card-icon img {
  animation: rotate360 1s cubic-bezier(0.4, 0, 0.2, 1);
}

@keyframes rotate360 {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}

/* 修改卡片图标悬停效果 */
.service-card:hover .card-icon {
  transform: scale(1.1);
  background: rgba(255, 255, 255, 0.3);
}

/* 添加连续旋转动画的类 */
.card-icon img.continuous-rotate {
  animation: rotate360 2s linear infinite;
}

/* 修改点击效果 */
.service-card:active .card-icon {
  transform: scale(0.95);
}

.service-card:active .card-icon img {
  animation: none; /* 点击时停止旋转 */
}

/* 添加点击波纹效果 */
.service-card::after {
  content: '';
  position: absolute;
  top: 50%;
  left: 50%;
  width: 5px;
  height: 5px;
  background: rgba(255, 255, 255, 0.5);
  opacity: 0;
  border-radius: 100%;
  transform: scale(1, 1) translate(-50%);
  transform-origin: 50% 50%;
}

.service-card:active::after {
  animation: ripple 0.6s ease-out;
}

@keyframes ripple {
  0% {
    transform: scale(0, 0);
    opacity: 0.5;
  }
  20% {
    transform: scale(25, 25);
    opacity: 0.3;
  }
  100% {
    opacity: 0;
    transform: scale(40, 40);
  }
}

/* 优化卡片内容动画 */
.service-card:hover .card-content {
  transform: translateX(5px);
}

/* 优化 emoji 动画 */
.service-card:hover .emoji-icons {
  animation: wave 1s infinite;
}

/* 优化状态图标动画 */
.service-card:hover .status {
  animation: bounce 0.6s infinite;
}
</style>
